<?php
/**
 * 首页控制器
 * @author cjli
 *
 */
class IndexAction extends HomeAction 
{
	public function _initialize()
	{
		parent::_initialize();
	}

	public function test(){
		$v = "315585451@qq.com";
		$title = "测试";
		$content = "测试邮件";
		$mailInstance = new EmailModel();
		$mailInstance->sendMail($v, $title, $content);
	}
	/**
	public function ceshi(){
		$config =array(
			'DB_TYPE' => 'mysql',								
			'DB_HOST' => 'localhost',						
			'DB_NAME' => 'ownonlynew',									
			'DB_USER' => 'root',									
			'DB_PWD' => '123',										
			'DB_PORT' => '3306',									
			'DB_PREFIX' => ''
		);
		$config1 =array(
			'DB_TYPE' => 'mysql',								
			'DB_HOST' => 'localhost',						
			'DB_NAME' => 'oocn_new',									
			'DB_USER' => 'root',									
			'DB_PWD' => '123',										
			'DB_PORT' => '3306',									
			'DB_PREFIX' => ''
		);
		$product_db = new Model('product',' ',$config);
		$product_attribute_db = new Model('product_attribute',' ',$config);
		$results = $product_db->field('id,show_thread_color,show_attr_ids')->select();
		foreach($results as &$val){
			$val['attr']=$product_attribute_db->where(array("product_id"=>$val['id']))->select();
		}
		$array=array();
		$product_new_db = new Model('product',' ',$config1); 
		$product_attribute_new_db = new Model('product_attribute',' ',$config1); 
		foreach($results as $v){
			$product = $product_new_db->where(array("eng_id"=>$v['id']))->find();
			if(isset($product['id']) && !empty($product['id'])){
				$data['show_attr_ids']   	 = $v['show_attr_ids'];
				$data['show_thread_color']   = $v['show_thread_color'];
				$product_new_db->where(array("eng_id"=>$v['id']))->save($data);
				$res=$product_new_db->where(array("eng_id"=>$v['id']))->find();
				if(isset($v['attr']) && is_array($v['attr']) && count($v['attr'])>0){
					foreach($v['attr'] as $va){
						unset($va['product_id']);
						$va['product_id'] = $res['id'];
						$product_attribute_new_db->add($va);
					}
				}
			}else{
				array_push($array,$v['id']);	
			}
		}
		print_r($array);exit;
	}
	**/
    public function index()
    {
        // if(!cookie('ncr')){
        //     //ownonly首页国内ip访问跳转至www.ownonly.cn 
        //     //访问www.ownonly.com/ncr 同样访问首页但不根据Ip跳转
        //     //$ip = "98.126.140.226";//US
        //     //$ip = "125.119.109.246";//CN
        //     $ip = get_client_ip();
        //     //加载IP库
        //     include(VENDOR_PATH."GeoIp/geoip.inc");
        //     $gi = geoip_open(VENDOR_PATH."GeoIp/GeoIP.dat",GEOIP_STANDARD);
        //     $geo_code = geoip_country_code_by_addr($gi, $ip);
        //     $getUrl = preg_replace('/\?l=\w+&?/','',$_SERVER['REQUEST_URI']);
        //     if($getUrl == '/ncr'){
        //         cookie('ncr', 'on','expire=2592000&path=/&domain='.APP_DOMAIN);
        //     }
        //     if($geo_code == 'CN' && $getUrl != '/ncr'){
        //         send_http_status(301);
        //         Header("Location: http://www.ownonly.cn");
        //         exit;     
        //     }
        // }
        
        //3d测试
        $getUrl = preg_replace('/\?l=\w+&?/','',$_SERVER['REQUEST_URI']);
        if($getUrl == '/3d'){
            cookie('3dshow', 'on','expire=2592000&path=/&domain='.APP_DOMAIN);
        }
        if($getUrl == '/3d0'){
            cookie('3dshow', 'on','expire=-1&path=/&domain='.APP_DOMAIN);
        }

    	$data = array(
    		'docTitle' => L('meta_title'),
    		'docKeywords' => L('meta_keywords'),
    		'docDescription' => L('meta_description'),
    	);
//    	//热销产品
//    	$hotProductList = ProductModel::get_product_list(array('recommond_id' => 13), 0, 20);
//    	$data['hotProductList'] = $hotProductList;
//    	//新品
//    	$newProductList = ProductModel::get_product_list(array('recommond_id' => 11), 0, 20);
//    	$data['newProductList'] = $newProductList;
      	//轮播广告
  	    $slideList = AdvModel::get_slide_list();
     	$data['slideList'] = $slideList;//print_r($data['slideList']);
    	$this->assign($data);
    	if(check_wap()||$_GET['mobile']==1){
    	    $this->assign('mobile',1);
    	}
		$this->display('index_real');
    }
	public function index_real()
    {
    	$data = array(
    		'docTitle' => L('meta_title'),
    		'docKeywords' => L('meta_keywords'),
    		'docDescription' => L('meta_description'),
    	);
//    	//热销产品
//    	$hotProductList = ProductModel::get_product_list(array('recommond_id' => 13), 0, 20);
//    	$data['hotProductList'] = $hotProductList;
//    	//新品
//    	$newProductList = ProductModel::get_product_list(array('recommond_id' => 11), 0, 20);
//    	$data['newProductList'] = $newProductList;
//    	//轮播广告
//    	$slideList = AdvModel::get_slide_list();
//    	$data['slideList'] = $slideList;
    	$this->assign($data);
		$this->display('index_real');
    }

    public function shopping(){
		$data = array(
    		'docTitle' => L('meta_title_shopping'),
    		//'docKeywords' => L('meta_keywords_shopping'),
    		'docDescription' => L('meta_description_shopping'),
    	);
		$this->assign($data);		
        $this->display('shop/shopping_map');
    }
	
    public function country()
    {
		$data = array(
    		'docTitle' => L('meta_title_country'),
    		'docKeywords' => L('meta_keywords'),
    		'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->assign('robots','noindex,nofollow');
		$this->display('country');
    }
	
    public function error()
    {
    	$this->display(TPL_PATH.'public/404.phtml');
    }


    public function feedback(){
        if($_POST['email']){

            $email = $_POST['email'];
            $message = $_POST['message'];

            $post = array(
                'message_type' => 1,
                'email' => $email,
                'username' => $email,
                'title' => 'User Feedback',
                'content' => $message,
            );
            if(CommentModel::insert_guestbook($post)){
                //内部邮件
                $sendMail = 'service@ownonly.net';
                $title = 'User Feedback';
                $content = 'Email:&nbsp;'.$post['email'].'<br />';
                $content .= 'Message:'.$post['content'].'<br />';
                $mailInstance = new EmailModel();
                $mailInstance->sendMail($sendMail, $title, $content);
                $info = array(
                    'errno' => 1,
                    'errstr' => L('_OPERATION_SUCCESS_'),
                );
            }else{
                $info = array(
                    'errno' => 0,
                    'errstr' => L('_OPERATION_FAIL_'),
                );
            }
            echo json_encode($info);exit;
        }
    }

    public function _empty(){
        $this->read(ACTION_NAME);
    }

    //url路由访问到这里
    public function read($link){
        global $language_id;

        $link = htmlspecialchars($link);;
        $where['url_path'] = $link;
        $where['language_id'] = $language_id;
        $info = M('collection')->where($where)->find();

        if(!$info){
            send_http_status(404);
            $this->display(C('ERROR_PAGE'));
            exit;
        }

        if($link == 'staywithus' || $link == 'spring-sale'){
            send_http_status(301);
            redirect('/');
            exit;
        }

        $data = array(
            'docTitle' => $info['meta_title'],
            'docKeywords' => $info['meta_keywords'],
            'docDescription' => $info['meta_description'],
        );

        $collection_info = M('collection_items')
        ->where("img_language_id = ".$language_id." and collection_id=".$info['collection_id'])
        ->order('banner_id')->select();

        foreach($collection_info as $k=>$v){
            $v['pids'] = trim($v['pids'],',');
            $collection_info[$k]['ids'] = explode(',',$v['pids']);
            $product_list[$k] = ProductModel::get_product_collection_list($collection_info[$k]['ids'],$language_id);
        }

        if(htmlspecialchars($link) == 'staywithus'){
            cookie('self_coupon', 'staywithus','expire=604800&path=/&domain='.APP_DOMAIN);
            $this->assign('target_blank', 1);
        }
	
        $this->assign('target_blank', 1);
	
        if(htmlspecialchars($link) == 'spring-sale'){
            cookie('self_coupon', 'spring25','expire=604800&path=/&domain='.APP_DOMAIN);
            $this->assign('target_blank', 1);
        }

        $this->assign('link',trim($link));
        $this->assign($data);
        $this->assign('info',$info);
        $this->assign('product_list',$product_list);
        $this->assign('collection_info',$collection_info);

        $this->display('collection/read');
    }
}